<html>
<head><meta charset="utf-8"><title>transmute between generic enums · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html">transmute between generic enums</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218958520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218958520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218958520">(Dec 05 2020 at 23:16)</a>:</h4>
<p>I am currently trying to use an enum like</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">enum</span> <span class="nc">Foo</span><span class="o">&lt;</span><span class="n">P</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Bar</span><span class="p">(</span><span class="kt">u32</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="n">Zar</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Var</span><span class="p">(</span><span class="n">P</span><span class="p">),</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="218958530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218958530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218958530">(Dec 05 2020 at 23:17)</a>:</h4>
<p>if I now have <code>Foo&lt;!&gt;</code> and <code>Foo&lt;SmallTy&gt;</code> so that the <code>Size</code> of <code>Foo</code> is the same in both cases</p>



<a name="218958538"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218958538" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218958538">(Dec 05 2020 at 23:17)</a>:</h4>
<p>can i soundly transmute between the two if they are a different variant to <code>Foo::Var</code>?</p>



<a name="218958589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218958589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218958589">(Dec 05 2020 at 23:18)</a>:</h4>
<p>i kind of don't think so but i would be interested in seeing any potential approaches worth trying here</p>



<a name="218958605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218958605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218958605">(Dec 05 2020 at 23:19)</a>:</h4>
<p>I intend to only use this inside of <code>rustc</code> tiself, so I wouldn't mind being forced to rely on some publicly unspecified implementation detail</p>



<a name="218958658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218958658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218958658">(Dec 05 2020 at 23:20)</a>:</h4>
<p>as long as we are able to reason about said implementation detail without too much effort</p>



<a name="218959094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218959094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218959094">(Dec 05 2020 at 23:33)</a>:</h4>
<p>You could give it a <code>#[repr(...)]</code>, such as <code>C</code> or <code>u8</code>. In that case it should be sound to transmute</p>



<a name="218959471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218959471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218959471">(Dec 05 2020 at 23:45)</a>:</h4>
<p>See <a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#layout-of-a-data-carrying-enums-with-an-explicit-repr-annotation">https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#layout-of-a-data-carrying-enums-with-an-explicit-repr-annotation</a> (it's non-normative yet, but definitely stable enough for <code>rustc</code>)</p>



<a name="218960319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218960319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218960319">(Dec 06 2020 at 00:09)</a>:</h4>
<p>Given that repr(rust) it can't optimize away the discriminant in any of the cases anyway, I think the <code>#[repr(C, u8)]</code> kind of solution is probably best.  Doesn't introduce extra cost, and doesn't rely on unspecified details.</p>



<a name="218963760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/transmute%20between%20generic%20enums/near/218963760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matt1992 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/transmute.20between.20generic.20enums.html#218963760">(Dec 06 2020 at 01:54)</a>:</h4>
<p><span class="user-mention silent" data-user-id="232018">Daniel Henry-Mantilla</span> <a href="#narrow/stream/122651-general/topic/transmute.20between.20generic.20enums/near/218959471">said</a>:</p>
<blockquote>
<p>See <a href="https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#layout-of-a-data-carrying-enums-with-an-explicit-repr-annotation">https://rust-lang.github.io/unsafe-code-guidelines/layout/enums.html#layout-of-a-data-carrying-enums-with-an-explicit-repr-annotation</a> (it's non-normative yet, but definitely stable enough for <code>rustc</code>)</p>
</blockquote>
<p>Don't know how recently this was added to the reference, <br>
but <code>#[repr(Int)]</code> and <code>#[repr(C, Int)]</code> enums with fields are both in the stable reference already:<br>
<a href="https://doc.rust-lang.org/reference/type-layout.html?highlight=repr,inet#primitive-representation-of-enums-with-fields">https://doc.rust-lang.org/reference/type-layout.html?highlight=repr,inet#primitive-representation-of-enums-with-fields</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>